﻿using System;
using System.Collections.Generic;
using System.Text;
using Database;
using System.Data;

namespace ObjectManager
{
    class DangNhapDAO : IDatabase
    {
        private ConnectionDB cnn = null;

        public DangNhapDTO DangNhapDTO { get; set; }

        public DangNhapDAO()
        {
            cnn = Connection.Connect();
            DangNhapDTO = new DangNhapDTO();
        }

        private int Quyen()
        {
            DataTable temp = cnn.QueryProcedure("TimMaQuyenDangNhap", DangNhapDTO.TenQuyen);
            int MaBM = 0;
            if (temp != null && temp.Rows.Count > 0 && !string.IsNullOrEmpty(temp.Rows[0][0].ToString()))
            {
                MaBM = int.Parse(temp.Rows[0][0].ToString());
                DangNhapDTO.Quyen = MaBM;
            }
            return MaBM;
        }

        public int Insert()
        {
            if (!string.IsNullOrEmpty(DangNhapDTO.TenDangNhap) && !string.IsNullOrEmpty(DangNhapDTO.MatKhau))
            {
                return cnn.ExecuteProcedure("InsertDangNhap", DangNhapDTO.TenDangNhap, Security.GetMd5(DangNhapDTO.MatKhau), DangNhapDTO.Ten, Quyen());
            }
            return -1;
        }

        public int Update()
        {
            if (!string.IsNullOrEmpty(DangNhapDTO.TenDangNhap))
            {
                return cnn.ExecuteProcedure("UpdateDangNhap", DangNhapDTO.TenDangNhap, DangNhapDTO.Ten, Quyen());
            }
            return -1;
        }

        public int UpdatePassword()
        {
            if (!string.IsNullOrEmpty(DangNhapDTO.TenDangNhap))
            {
                return cnn.ExecuteProcedure("UpdateDangNhap_MatKhau", DangNhapDTO.TenDangNhap, Security.GetMd5(DangNhapDTO.MatKhau));
            }
            return -1;
        }

        public int UpdatePassword(string matKhau)
        {
            if (!string.IsNullOrEmpty(DangNhapDTO.TenDangNhap))
            {
                return cnn.ExecuteProcedure("DoiMatKhau", DangNhapDTO.TenDangNhap, Security.GetMd5(DangNhapDTO.MatKhau), Security.GetMd5(matKhau));
            }
            return -1;
        }

        public int Delete()
        {
            if (!string.IsNullOrEmpty(DangNhapDTO.TenDangNhap))
            {
                return cnn.ExecuteProcedure("DeleteDangNhap", DangNhapDTO.TenDangNhap);
            }
            return -1;
        }

        public int ChangePassword(string Old_Password, string New_Password)
        {
            if (!string.IsNullOrEmpty(DangNhapDTO.TenDangNhap))
            {
                int check = cnn.ExecuteProcedure("DoiMatKhau", DangNhapDTO.TenDangNhap, Security.GetMd5(Old_Password), Security.GetMd5(New_Password));
                if (check > 0)
                {
                    DangNhapDTO.MatKhau = New_Password;
                }
                return check;
            }
            return -1;
        }

        public bool Check()
        {
            if (!string.IsNullOrEmpty(DangNhapDTO.TenDangNhap) && !string.IsNullOrEmpty(DangNhapDTO.MatKhau))
            {
                DataTable info = cnn.QueryProcedure("ThongTinDangNhap", DangNhapDTO.TenDangNhap, Security.GetMd5(DangNhapDTO.MatKhau));
                if (info != null && info.Rows.Count > 0)
                {
                    DangNhapDTO.Ten = info.Rows[0]["Ten"].ToString();
                    DangNhapDTO.Quyen = int.Parse(info.Rows[0]["Quyen"].ToString());
                    DangNhapDTO.TenQuyen = info.Rows[0]["TenQuyen"].ToString();
                    return true;
                }
            }
            return false;
        }

        public DataTable ListAll()
        {
            return cnn.Query("select * from vw_DangNhap;");
        }

        public DataTable ListAll(string TenDangNhap, string Ten, string Quyen)
        {
            return cnn.Query("select * from vw_DangNhap where TenDangNhap like {0} and Ten like {1} and TenQuyen like {2}", TenDangNhap, Ten, Quyen);
        }

        public string[] ListName()
        {
            string[] result = null;
            DataTable temp = cnn.Query("select distinct TenQuyen from vw_DangNhap;");
            if (temp != null && temp.Rows.Count > 0)
            {
                result = new string[temp.Rows.Count];
                for (int i = 0; i < temp.Rows.Count; i++)
                {
                    result[i] = temp.Rows[i][0].ToString();
                }
            }
            return result;
        }
    }
}
